package com.dragon.datastructure.linkedlist;

/**
 * 求单链表中有效节点的个数
 *
 * @author dragon
 * @since 2019/12/17
 */
public class Test1 {

    private static class Node {
        Node next;
        int val;
    }

    /**
     * 获得单链表中的有效节点个数
     *
     * @param head 头节点
     * @return 节点个数
     */
    public static int getLength(Node head) {
        if (head == null) {
            return 0;
        }
        int count = 0;
        Node cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }

        return count;
    }
}



